package com.framerock.baselib;

import com.framerock.db.DB;
import com.framerock.db.DBConn;
import com.framerock.server.Request;

import java.util.ArrayList;
import java.util.List;

public class Base {

    protected static List<DBConn> dbArr     = new ArrayList<DBConn>();
    protected static List<Request> reqArr   = new ArrayList<Request>();

    protected Request mRequest  = null;
    protected DBConn nDb        = null;
    protected int dbsort        = -1;


    protected void initBase()
    {
        this.nDb      = this.dbCont();
        this.mRequest = reqArr.get(this.dbsort);
    }

    /**
     * 当前数据库连接
     * */
    protected DBConn dbCont()
    {
        if(this.dbsort==-1)this.dbsort = Base.dbContcreate();
        DBConn db = dbArr.get(this.dbsort);
        return db;
    }


    /**
     * 创建数据库连接
     * */
    public static int dbContcreate()
    {
        dbArr.add(DB.getDbobj());
        reqArr.add(null);
        return dbArr.size()-1;
    }

    /**
     * 关闭
     * */
    public static void dbContClose(int xu)
    {
        DBConn db = dbArr.get(xu);
        if(db!=null)db.close();
        dbArr.set(xu, null);
        reqArr.set(xu, null);
    }

    /**
     * 设置参数
     * */
    public static void setRequest(int xu, Request req)
    {
        reqArr.set(xu, req);
    }
}
